package com.gs.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.filechooser.FileSystemView;

import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class ConfigUtil {
	public static List<String> getMyEclipsePathBySigar(){
		List<String> listPath =new  ArrayList<String>() ;
		String[] cmd = {"tasklist"};
	    try {
	        Process process = Runtime.getRuntime().exec(cmd);
	        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
	        String string_temp = "";
	        while ((string_temp = in.readLine()) != null) {
	            //在控制台中打印进程信息
	           // System.out.println(string_temp);
	            if(string_temp.indexOf("eclipse.exe") != -1||string_temp.indexOf("myeclipse.exe")!=-1) {
	                //截取进程PID
	                String pid = string_temp.substring(28, string_temp.indexOf("Console")).trim();
	                System.out.println("----进程pid:" + pid);
	                //System.out.println("pid结束:" + string_temp.indexOf("Console"));
	                Sigar sigar = new Sigar();
	                //获取进程路径
	                String processPath = sigar.getProcExe(pid).getName();
	               // System.out.println("----进程路径:" + processPath);
	                processPath=processPath.substring(0,processPath.lastIndexOf("\\"));
	                System.out.println("----进程路径:" + processPath);
	                listPath.add(processPath);
	            }
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
		return listPath;
		
	}
	
	public static List<String> getMyEclipsePath(){
		
		List<String> listPath =new  ArrayList<String>() ;
		FileSystemView fsv = FileSystemView.getFileSystemView();  
		LnkParser lnk=new LnkParser();
		String desktopPath=fsv.getHomeDirectory().getPath();
		File file = new File(desktopPath);
		if (file.exists()) {
			File[] files = file.listFiles();
			if (files.length == 0) {
				System.out.println("文件夹是空的!");

			} else {
				for (File file2 : files) {
					if (file2.isDirectory()) {
						//System.out.println("文件夹:" + file2.getAbsolutePath());
						//  traverseFolder2(file2.getAbsolutePath());
					} else {
						
						if(file2.getAbsolutePath().indexOf("myeclipse.exe.lnk")!=-1) {
							try {
								System.out.println("文件:" + file2.getAbsolutePath());
								 
								String path=lnk.getRealFilename(file2);
								path=path.substring(0,path.lastIndexOf("\\"));
								System.out.println("path:"+path);
								listPath.add(path);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		} else {
			System.out.println("文件不存在!");
		} 

		/* String commond = "HKEY_CURRENT_USER\\Software\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache";
	        try {
	            Process ps = null;  
	            ps = Runtime.getRuntime().exec("reg query "+commond.replaceAll(" ", "\" \""));
	            ps.getOutputStream().close();  
	            InputStreamReader i = new InputStreamReader(ps.getInputStream(),"GBK");  
	            String line; 
	            String value;
	            String key;
	            BufferedReader ir = new BufferedReader(i); 
	            //listPath=new List<>();
	            //listPath=new List<String>(); 
	            listPath=new ArrayList<String>();
	            while ((line = ir.readLine()) != null) {  
	                if(line.indexOf("eclipse")!=-1)
	                if(line.indexOf("    REG_SZ    ")>0){
	                	key=line.substring(0,line.indexOf("    REG_SZ    "));
	                	value=line.substring(line.indexOf("    REG_SZ    ")+14);
	                	//System.out.println("key===》"+key);
	                	//System.out.println(value);
	                	String path=key.substring(4,key.lastIndexOf("\\"));
	                	//listPath.
	                	System.out.println("eclipse path=====>"+path);
	                	listPath.add(path);
	                }		            
	            }  
	        } catch (IOException e) {  

	            e.printStackTrace();  
	        } */ 
		return listPath;
	}
	
	

	public static List<String> getWorkspaces(String path){
		String prefsPath=path+"\\configuration\\.settings\\org.eclipse.ui.ide.prefs";
		List<String> list=new ArrayList<String>();
		File file=new File(prefsPath);
		if(file.exists()&&!file.isDirectory()){
			System.out.println("文件存在");
			try {
				FileReader fr=new FileReader(file);
				BufferedReader br = new BufferedReader(fr);  
				String line;// = br.readLine();   
				while ((line = br.readLine()) != null) {
					//System.out.println(line);
					if(line.indexOf("\\:")!=-1){
						String str []  = line.replaceAll("RECENT_WORKSPACES=", "").split("\\\\n");
						for(int i=0;i<str.length;i++){
							list.add(str[i].replaceFirst("\\\\", ""));
							System.out.println(str[i].replaceFirst("\\\\", ""));
						}
						//list.add(line.replaceAll("RECENT_WORKSPACES=", ""));

						//System.out.println("f>"+line.replaceAll("RECENT_WORKSPACES=", ""));
					}
					//line = br.readLine();   
				}   
				br.close();   
				fr.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}else{
			System.out.println("文件不存在");

		}
		return list;
	}

	public static List<String> getProjects(String path) {
		List<String> projects=new ArrayList<String>();
		File file=new File(path);
		if (file.exists()) {
			File[] files = file.listFiles();
			if (files.length>0) {
				for (File file2 : files) {
					if (file2.isDirectory()) {
						File[] files2 = file2.listFiles();
						if (files2.length>0) {
							if(files2[0].getName().equals(".classpath")) {
								projects.add(file2.getName());
							}else {
								
								System.out.println(file2+"-不是项目文件夹---");
								/*for (File file3 : files2) {
									System.out.println("			"+file3.getName());
								}*/
							}
						}
					}
				}
			} else {
				System.out.println("该工作空间没有项目");
			}
		}else {
			System.out.println("工作空间不存在");
		}
		return projects;
	}
}
